<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<%@include file="/WEB-INF/views/include/treetable.jsp" %>
<table id="treeTable" class="table table-striped table-bordered table-condensed">
	<thead>
	<tr>
		<td>选择分类</td>
	</tr>
	</thead>
	<tbody id="treeTableList"></tbody>
</table>
<script type="text/template" id="treeTableTpl">
	<tr id="{{row.id}}" pId="{{pid}}">
		<td><a href="javascript:void" >{{row.name}}</a></td>
	</tr>
</script>

<script type="text/javascript">
	$(document).ready(function() {
		var tpl = $("#treeTableTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
		var data = ${fns:toJson(list)}, ids = [], rootIds = [];
		for (var i=0; i<data.length; i++){
			ids.push(data[i].id);
		}
		ids = ',' + ids.join(',') + ',';
		for (var i=0; i<data.length; i++){
			if (ids.indexOf(','+data[i].parentId+',') == -1){
				if ((','+rootIds.join(',')+',').indexOf(','+data[i].parentId+',') == -1){
					rootIds.push(data[i].parentId);
				}
			}
		}
		for (var i=0; i<rootIds.length; i++){
			addRow("#treeTableList", tpl, data, rootIds[i], true);
		}
		$("#treeTable").treeTable({expandLevel : 5});
	});
	function addRow(list, tpl, data, pid, root){
		for (var i=0; i<data.length; i++){
			var row = data[i];
			if ((${fns:jsGetVal('row.parentId')}) == pid){
				$(list).append(Mustache.render(tpl, {
					dict: {
						blank123:0}, pid: (root?0:pid), row: row
				}));
				addRow(list, tpl, data, row.id);
			}
		}
	}
</script>